package com.ujiuye.controller;

import com.ujiuye.bean.User;
import com.ujiuye.service.DeleteService;
import com.ujiuye.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("UTF-8");
        String uid = req.getParameter("uid");
        if (uid!=null){
            System.out.println(uid);
            DeleteService deleteService = new DeleteService();
            boolean ds1 = deleteService.ds(uid);
            UserService use1 = new UserService();
            List<User> users1 = use1.selectAllUser();
            req.setAttribute("ulist",users1);
            req.getRequestDispatcher("show.jsp").forward(req,resp);
        }else{
            String username = req.getParameter("username");
            String pwd = req.getParameter("pwd");
            UserService us=new UserService();
            User user=us.getUserByNameAndPassword(username,pwd);
            if(user!=null){
                HttpSession session = req.getSession();
                session.setAttribute("user",user);
                resp.sendRedirect("show");
            }else {
                req.setAttribute("mes","用户名或者密码错误");
                req.getRequestDispatcher("login.jsp").forward(req,resp);
            }
        }
    }
}
